package com.libtop.scauicapp;

import android.annotation.SuppressLint;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceView;
import android.view.View;
import android.view.WindowInsets;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.libtop.scauicapp.databinding.ActivityMainBinding;
import com.libtop.scauicapp.face.CircleCanvasView;
import com.libtop.scauicapp.utils.QrcodeUtil;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.TimeZone;

public class MainActivity extends AppCompatActivity {
    private static final int TIME_MESSAGE_KEY = 1;
    private View mContentView;
    private TextView txtDateTime;
    private TextView txtRoomName;
    private TextView txtRoomStatus;
    private TextView txtFaceRecognition;
    private TextView txtPassword;
    private ImageView imgIcQrcode;
    private ActivityMainBinding binding;
    private RelativeLayout layoutFaceRecognition;
    private RelativeLayout layoutInputPassword;
    private CircleCanvasView circleCanvasView;
    private SurfaceView viewFaceDetector;
    private RelativeLayout layoutFaceDetector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityMainBinding.inflate(getLayoutInflater());
        mContentView = binding.getRoot();
        setContentView(binding.getRoot());
        if (Build.VERSION.SDK_INT >= 30) {
            mContentView.getWindowInsetsController().hide(
                    WindowInsets.Type.statusBars() | WindowInsets.Type.navigationBars());
        } else {
            mContentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
                    | View.SYSTEM_UI_FLAG_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
        }

        txtDateTime = findViewById(R.id.txtDateTime);

        new TimeThread().start();

        txtRoomName = findViewById(R.id.txtRoomName);

        txtRoomName.setText("外文书库/中文艺术类书库");
        txtRoomStatus = findViewById(R.id.textRoomStatus);
        txtRoomStatus.setText("正常");

        txtFaceRecognition = findViewById(R.id.txtFaceRecognition);
        txtFaceRecognition.setText("人脸识别");

        txtPassword = findViewById(R.id.txtPassword);
        txtPassword.setText("输入密码");
        imgIcQrcode = findViewById(R.id.imgIcQrcode);

        // 生成开门二维码
        try {
            this.createQrcode("测试", 190, 190);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        // 人脸识别点击切换
        layoutFaceRecognition = findViewById(R.id.layoutFaceRecognition);
        layoutFaceRecognition.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.w("face recognition", "Clicked");
            }
        });

        // 输入密码点击切换
        layoutInputPassword = findViewById(R.id.layoutInputPassword);

        layoutInputPassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.w("input password", "Clicked");
            }
        });

        layoutFaceDetector = findViewById(R.id.layoutFaceDetector);
        addMasking(layoutFaceDetector);

    }
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY年MM月DD日 HH:mm:ss");
    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
    }
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case TIME_MESSAGE_KEY:
                    txtDateTime.setText(getTime());
                    break;
                default:
                    break;
            }
        }
    };

    //获得当前年月日时分秒星期
    public String getTime() {
        final Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        String mDate = new SimpleDateFormat("YYYY.MM.dd").format(c.getTime());
        String mHour = String.format("%02d", c.get(Calendar.HOUR_OF_DAY));
        String mMinute = String.format("%02d", c.get(Calendar.MINUTE));
        String mSecond = String.format("%02d", c.get(Calendar.SECOND));

        int weekDay = c.get(Calendar.DAY_OF_WEEK);
        String mWeek = "周";
        switch (weekDay) {
            case 2:
                mWeek += "一";
                break;
            case 3:
                mWeek += "二";
                break;
            case 4:
                mWeek += "三";
                break;
            case 5:
                mWeek += "四";
                break;
            case 6:
                mWeek += "五";
                break;
            case 7:
                mWeek += "六";
                break;
            case 1:
                mWeek += "日";

        }
        //return mYear + "年" + mMonth + "月" + mDay + "日" + " " + "星期" + mWay + " " + mHour + ":" + mMinute + ":" + mSecond;
        return mDate + " " + mWeek + " " + mHour + ":" + mMinute + ":" + mSecond;
    }
    public class TimeThread extends Thread {
        @Override
        public void run() {
            try {
                do {
                    Thread.sleep(1000);
                    Message message = new Message();
                    message.what = TIME_MESSAGE_KEY;
                    mHandler.sendMessage(message);
                } while (true);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

    private void createQrcode(String contents, int width, int height) throws IOException {
        imgIcQrcode.setImageBitmap(QrcodeUtil.generateQrCode(contents, width, height));
    }

    private void addMasking(RelativeLayout view) {
        circleCanvasView = new CircleCanvasView(this);
        view.addView(circleCanvasView);
    }
}